﻿
@{
    ViewBag.Title = "Commodity";
    Layout = "~/Views/Shared/_ManagerLayout.cshtml";
}
@section style{
<style>
    th, td {
        text-align: center;
    }
</style>
}

<div class="tabbable" id="tabs">
    <ul class="nav nav-tabs">
        <li class="active">
            <a href="#panel" data-toggle="tab" onclick="loadOrders(1, 1, 0)">出售中</a>
        </li>
        <li>
            <a href="#panel" data-toggle="tab" onclick="loadOrders(2, 1, 0)">未上架</a>
        </li>
        <li>
            <div style="margin-left:50px; margin-top:13px;">
                筛选条件
            </div>
        </li>
        <li>
            <div style="margin-left:10px;margin-top:5px;">
                <select class="form-control" id="commodityType">
                    <option value="0">全部</option>
                    <option value="1">男装</option>
                    <option value="2">女装</option>
                    <option value="3">玩具</option>
                </select>
            </div>
        </li>
        <li>
            <div style=" margin-top:6px;">
                <button class="btn btn-success" onclick="serch()">搜索</button>
            </div>
        </li>
    </ul>
    <div class="tab-content">
        <div class="tab-pane active" id="panel">
            <table id="orderList" class="table table-striped">
                <thead>
                    <tr>
                        <th style="width:60%;">商品名称</th>
                        <th style="width:10%">单价</th>
                        <th style="width:10%">数量</th>
                        <th style="width:20%">操作</th>
                    </tr>
                </thead>
                <tbody></tbody>
            </table>
            <ul id="paging" class="pagination"></ul>
        </div>
    </div>
</div>

@section script{
    <script>
    var _state = 0;
    var _page = 0;
    var _type = 0;

    $(function () {
        $("#Commodity").addClass("active");
        loadOrders(1, 1, 0);
    });

    function loadOrders(state, page, type) {
        if (state != _state || page != _page || _type != type) {
            if (page == 0) page = 1;//预留强制刷新
            _state = state;
            _type = type;
            $.ajax({
                type: "POST",
                url: "/Manager/GetCommodityList",
                data: { state: state, page: page, type:type },
                dataType: "json",
                success: function (data) {
                    loadTable(data.List);
                    loadPaging(data);
                }
            });
        }
    }
    function serch() {
        loadOrders(_state, 1, $("#commodityType").val());
    }
    function loadTable(list) {
        $('#orderList tbody').empty();
        for (var i in list) {
            var row = "<tr>"
                + "<td style=' text-align:left'><a target=\"_blank\" href='/Home/Details?id=" + list[i].Id + "'>" + list[i].Name + "</a></td>"
                + "<td>" + list[i].Price + "</td>"
                + "<td>" + list[i].Number + "</td>"
                + "<td>"
                + (list[i].State == 2
                    ? "<a href='javascript:void(0)' onclick = 'add(\"" + list[i].Id + "\")' class='btn btn-default'>上架<a>"
                    : "<a href='javascript:void(0)' onclick = 'remove(\"" + list[i].Id + "\")' class='btn btn-default'>下架<a>")
                + "<a href='/Manager/EditCommodity?id=" + list[i].Id + "' class='btn btn-default'>编辑商品信息<a>"
                + "</td>"
                + "</tr>";
            $('#orderList tbody').append(row);
        }
    }

    // 分页
    function loadPaging(paging) {
        _page = paging.Page;
        $('#paging').empty();
        var lis = "<li><a href='javascript:void(0)' onclick='loadOrders(" + _state + ", 1, " + _type + ")'>首页</a></li>";

        for (var i = 1; i <= paging.PageNumber; i++) {
            lis += "<li><a href='javascript:void(0)' onclick = 'loadOrders(" + _state + ", " + i + ", " + _type + ")'>" + i + "</a></li>";
        }

        lis += "<li><a href='javascript:void(0)' onclick='loadOrders(" + _state + ", " + paging.PageNumber + ", " + _type + ")'>尾页</a></li>";
        $('#paging').append(lis);
    }

    function add(id) {
        if (confirm("确认上架该商品吗？")) {
            $.ajax({
                type: "POST",
                url: "/Manager/AddCommodity",
                data: { id: id},
                dataType: "json",
                success: function (data) {
                    loadOrders(_state, 0) //0页 强制刷新
                }
            });
        }
    }
    function remove(id) {
        if (confirm("确认下架该商品吗？")) {
            $.ajax({
                type: "POST",
                url: "/Manager/RemoveCommodity",
                data: { id: id },
                dataType: "json",
                success: function (data) {
                    loadOrders(_state, 0) //0页 强制刷新
                }
            });
        }
    }
    </script>
}





